package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitOrgRelDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.dto.ResultWaitCheckFruitDTO;
import me.sdevil507.biz.fruit.po.FruitOrgRelPo;
import me.sdevil507.biz.fruit.service.FruitOrgRelService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.biz.screen.dto.ScreenActiveResultDTO;
import me.sdevil507.biz.screen.dto.ScreenSearchConditionDTO;
import me.sdevil507.biz.screen.service.ScreenService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @program: tips_back_end
 * @description: 成果展示类
 * @author: kzl
 * @create: 2020-06-23 16:39
 **/
@RestController
@RequestMapping("/api/org/fruit")
@Slf4j
public class FruitOrgController{

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FruitOrgRelService fruitOrgRelService;

    @Autowired
    private ScreenService screenService;

    /**
     * 机构待审核成果列表
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/readAllOrgFruitWaitingCheckByPage")
    @RequiresPermissions("org:fruit:check")
    public ApiResultDTO getAllWaitingCheckByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<ResultWaitCheckFruitDTO> pagePos = fruitService.readAllOrgFruitWaitingCheckByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 机构确定专家更新成果对应关系的状态--有效-或者无效
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_APPROVE, operDesc = "")
    @PostMapping("/confirmExpertFruitStatus")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO confirmExpertFruitStatus(@RequestBody FruitOrgRelDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            //拒绝 或 已经存在
            FruitOrgRelPo fruitOrgRelPo = fruitOrgRelService.findByCondition(dto.getFruitId(), OrgAccountHelper.getOrgId(), ComConstant.FruitStatus.WATICHECK);
            fruitOrgRelPo.setStatus(dto.getStatus());
            fruitOrgRelService.save(fruitOrgRelPo);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());

        } catch (Exception e) {
            //事务回滚
            log.error("操作成果", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "操作失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取机构主页的图表数据
     * @return
     */
    @PostMapping("/getOrgHomePercentData")
    public ApiResultDTO getHomePercentData() {
        ApiResultDTO apiResultDTO;
        //
        try {
            FruitDTO fruitDTO = new FruitDTO();
            fruitService.setBusinessId(fruitDTO);
            HashMap<String,Object> resultMap = fruitService.getOrgHomePercentData(fruitDTO);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);

        } catch (Exception e) {
            //事务回滚
            log.error("获取成果百分比数据失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果百分比数据失败");
        }
        return apiResultDTO;
    }
}
